package com.intpro.demo.Controller;

import com.intpro.demo.DAO.CommonRep;
import com.intpro.demo.DAO.HotSpot;
import com.intpro.demo.Service.HotSpotService;
import com.intpro.demo.Utils.CodeException;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("api/hotspot")
public class HotSpotController
{

	@Autowired
	private HotSpotService hotSpotService;

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public CommonRep add(@RequestBody Map<String, String> req) throws Exception
	{
		var hs = req.get("hs");
		HotSpot h = new HotSpot();
		h.index = 1;
		h.kw = hs;
		hotSpotService.add(h);
		return new CommonRep();
	}

	@RequestMapping(value = "changeIndex", method = RequestMethod.POST)
	public CommonRep changeIndex(@RequestBody Map<String, String> req) throws Exception
	{
		var id = req.get("id");
		var index = req.get("index");
		if (id == null || index == null)
		{
			throw CodeException.NotNullErr();
		}
		HotSpot h = hotSpotService.find(id);
		h.index = Integer.parseInt(index);
		hotSpotService.update(h);
		return new CommonRep();
	}

	@RequestMapping(value = "getAll", method = RequestMethod.POST)
	public CommonRep getAll() throws Exception
	{
		CommonRep c = new CommonRep();
		c.data = hotSpotService.getAll();
		return c;
	}

	@RequestMapping(value = "remove", method = RequestMethod.POST)
	public CommonRep remove(@RequestBody Map<String, String> req) throws Exception
	{
		var id = req.get("id");
		if (id == null)
		{
			throw CodeException.NotNullErr();
		}
		hotSpotService.remove(id);

		return new CommonRep();
	}

}
